package com.cloud.pole.rpc;

import com.ace.cache.annotation.Cache;
import com.cloud.pole.api.admin.AdminUserApi;
import com.cloud.pole.api.merchant.ShopMerchantInfoApi;
import com.cloud.pole.api.merchant.ShopMerchantUserApi;
import com.cloud.pole.api.permission.PermissionApi;
import com.cloud.pole.api.user.BaseUserApi;
import com.cloud.pole.common.constant.CommonConstants;
import com.cloud.pole.common.exception.BaseException;
import com.cloud.pole.common.exception.ParamException;
import com.cloud.pole.common.msg.ApiRestResponse;
import com.cloud.pole.common.util.jwt.JWTInfo;
import com.cloud.pole.merchant.model.entity.ShopMerchantInfo;
import com.cloud.pole.merchant.model.entity.ShopMerchantUser;
import com.cloud.pole.merchant.service.AuthService;
import com.cloud.pole.merchant.service.PermissionService;
import com.cloud.pole.merchant.service.ShopMerchantInfoService;
import com.cloud.pole.merchant.service.ShopMerchantUserService;

import lombok.extern.slf4j.Slf4j;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import java.util.Map;

/**
 * ${DESCRIPTION}
 *
 * @author wanghaobin
 * @create 2017-06-21 8:15
 */
@Slf4j
@RestController
@RequestMapping("api/user")
public class MerchantUserRest {
    @Autowired
    private ShopMerchantUserService userService;

	@RequestMapping(value = "/getUserByMobile", method = RequestMethod.GET)
	public ApiRestResponse<ShopMerchantUserApi> getUserByMobile(
			@RequestParam(name = "mobileArea", defaultValue = "") String mobileArea,
			@RequestParam(name = "mobile", defaultValue = "") String mobile) {
		try {
			ParamException.err(StringUtils.isBlank(mobileArea), "mobileArea不能为空");
			ParamException.err(StringUtils.isBlank(mobile), "mobile不能为空");
			ShopMerchantUser user = userService.getUserByMobile(mobileArea, mobile);
			ShopMerchantUserApi userApi = null;
			if (user != null) {
				userApi = new ShopMerchantUserApi();
				BeanUtils.copyProperties(user, userApi);
			}
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, userApi);
		} catch (BaseException e) {
			log.info(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}

	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public ApiRestResponse<Long> addUser(@RequestBody ShopMerchantUserApi userApi) {
		try {
			ParamException.err(userApi == null, "userApi不能为空");
			ShopMerchantUser user = new ShopMerchantUser();
			BeanUtils.copyProperties(userApi, user);
			userService.addUser(user);
			return ApiRestResponse.success(CommonConstants.STATUS_OK_MESSAGE, user.getUserId());
		} catch (BaseException e) {
			log.info(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(e.getStatus(), e.getMessage(), null);
		} catch (Exception e) {
			log.error(CommonConstants.STATUS_ERROR_MESSAGE + "{}", e.getMessage(), e);
			return ApiRestResponse.fail(CommonConstants.STATUS_ERROR_MESSAGE, null);
		}
	}
}
